package sample.stormax.pres.news.utils.sqlite;

import java.util.ArrayList;
import java.util.List;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

import sample.stormax.pres.news.beans.Data;

public class FavouriteSQLiteImpl implements ISQLiteOperation {

    private static final String TAG = "FavouriteSQLiteImpl";

    private MyHelper helper;
    private static ISQLiteOperation favouriteOp;

    public static FavouriteSQLiteImpl newInstance(Context context) {
        Log.d(TAG,"newInstance");
        favouriteOp = favouriteOp == null ? new FavouriteSQLiteImpl(context) : favouriteOp;
        return (FavouriteSQLiteImpl) favouriteOp;
    }

    private FavouriteSQLiteImpl(Context context) {
        helper = new MyHelper(context);
    }

    @Override
    public boolean addFavourite(Data data) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("uniquekey", data.getUniquekey());
        values.put("title", data.getTitle());
        values.put("date", data.getDate());
        values.put("category", data.getCategory());
        values.put("author_name", data.getAuthor_name());
        values.put("url", data.getUrl());
        values.put("thumbnail_pic_s", data.getThumbnail_pic_s());
        values.put("thumbnail_pic_s02", data.getThumbnail_pic_s02());
        values.put("thumbnail_pic_s03", data.getThumbnail_pic_s03());

        long result = db.insert(ISQLiteOperation.TABLE_NAME, null, values);
        db.close();
        if (result != -1) {
            Log.d(TAG,"added Favourite");
        }
        return result != -1;
    }

    @Override
    public boolean deleteFavourite(Data data) {
        SQLiteDatabase db = helper.getWritableDatabase();
        int result = db.delete(ISQLiteOperation.TABLE_NAME, "uniquekey=?",
                new String[]{data.getUniquekey()});
        db.close();
        Log.d(TAG,"deleted Favourite");
        return result >= 1 ;
    }

    @Override
    public void createTable() {
        helper.getWritableDatabase().close();
    }

    @Override
    public List<Data> queryAll() {
        SQLiteDatabase db = helper.getWritableDatabase();
        String[] columns = {"uniquekey", "title", "date", "category",
                "author_name", "url", "thumbnail_pic_s", "thumbnail_pic_s02",
                "thumbnail_pic_s03"};
        Cursor cursor = db.query(ISQLiteOperation.TABLE_NAME, columns, null, null, null, null, "uniquekey");
        if (cursor != null && cursor.getCount() > 0) {
            List<Data> listFavorites = new ArrayList<>();
            while (cursor.moveToNext()) {
                String uniquekey = cursor.getString(cursor
                        .getColumnIndex("uniquekey"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String date = cursor.getString(cursor.getColumnIndex("date"));
                String category = cursor.getString(cursor
                        .getColumnIndex("category"));
                String author_name = cursor.getString(cursor
                        .getColumnIndex("author_name"));
                String url = cursor.getString(cursor.getColumnIndex("url"));
                String thumbnail_pic_s = cursor.getString(cursor
                        .getColumnIndex("thumbnail_pic_s"));
                String thumbnail_pic_s02 = cursor.getString(cursor
                        .getColumnIndex("thumbnail_pic_s02"));
                String thumbnail_pic_s03 = cursor.getString(cursor
                        .getColumnIndex("thumbnail_pic_s03"));

                listFavorites.add(new Data(uniquekey, title, date, category,
                        author_name, url, thumbnail_pic_s, thumbnail_pic_s02,
                        thumbnail_pic_s03));
            }
            cursor.close();
            return listFavorites;
        }
        cursor.close();
        return null;
    }

}
